﻿package cn.poco.pepsi.util.xmlpared;

import java.io.InputStream;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;
import cn.poco.pepsi.bean.Topic;

public class TopicXmlparse {
	//private final static String TAG = "TopicXmlparse";
	private static ArrayList<Topic> topics = null;
	private static  Topic topicbean = null;
	
	public static ArrayList<Topic> getXml(InputStream inSteam) throws Exception{
		XmlPullParser xmpparse = Xml.newPullParser();   //得到解释器
		xmpparse.setInput(inSteam, "UTF-8");           //设置解释流
		int code = xmpparse.getEventType();            //得到节点信息
		String total = null;							//总记录数
		while(code!=XmlPullParser.END_DOCUMENT){
			switch (code) {
			case XmlPullParser.START_DOCUMENT:
				topics = new ArrayList<Topic>();
				break;
			case XmlPullParser.START_TAG:
				if("total".equals(xmpparse.getName())){
					total = xmpparse.nextText();
				}
				if("topic".equals(xmpparse.getName())){
					topicbean = new Topic();
					topicbean.setTotal(Integer.parseInt(total));
				}
				if(topicbean!=null){
					if("id".equals(xmpparse.getName())){
						String id = xmpparse.nextText();
						topicbean.setId(Integer.parseInt(id));
					}
					if("title".equals(xmpparse.getName())){
						String title = xmpparse.nextText();
						topicbean.setTitle(title);
					}
					if("top-image".equals(xmpparse.getName())){
						String image = xmpparse.nextText();
						topicbean.setImage(image);
					}
					if("discription".equals(xmpparse.getName())){
						String discription = xmpparse.nextText();
						topicbean.setDiscription(discription);
					}
				}
				break;
			case XmlPullParser.END_TAG:
				if("topic".equals(xmpparse.getName())){
					topics.add(topicbean);
					topicbean = null;
				}
				break;
			}	
			code = xmpparse.next();
		}
		inSteam.close();
		return topics;
	}
}
